﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.IO;

namespace Management
{
    public partial class MainWindow : System.Web.UI.Page
    {
        public Dao dao;

        protected void Page_Load(object sender, EventArgs e)
        {
            dao = new Dao();
            //dao.AddWeek(5);
            if (Request.Form["__EVENTTARGET"] == "removeGoal")
            {
                RemoveGoal(Request.Form["__EVENTARGUMENT"]);
            }
            else if (Request.Form["__EVENTTARGET"] == "removeTask")
            {
                RemoveTask(Request.Form["__EVENTARGUMENT"]);
            }
            else if (Request.Form["__EVENTTARGET"] == "changeStatus")
            {
                ChangeStatus(Request.Form["__EVENTARGUMENT"]);
            }
        }

        private void ChangeStatus(string task)
        {
            try
            {
                var items = task.Split('~');
                dao.ChangeTaskStatus(items[1], items[0]);
            }
            catch (Exception ex)
            {
                ShowException(ex.Message);
            }
        }

        private void RemoveTask(string data)
        {
            try
            {
                var goal = data.Split('~')[0];
                var task = data.Split('~')[1];
                dao.RemoveTask(task, goal);
            }
            catch (Exception ex)
            {
                ShowException(ex.Message);
            }
        }

        private void RemoveGoal(string goal)
        {
            try
            {
                dao.RemoveGoal(goal);
            }
            catch (Exception ex)
            {
                ShowException(ex.Message);
            }
        }

        private void ShowException(string exceptionTxt)
        {
            ScriptManager.RegisterStartupScript(this, GetType(),
            "showExceptionPop", "showExceptionPop('"+exceptionTxt+"');", true);
        }

        protected void addGoalBtn_OnClick(object sender, EventArgs e)
        {
            try
            {
                dao.AddGoal(newGoalName.Text);
            }
            catch (Exception ex)
            {
                ShowException(ex.Message);
            }
        }

        protected void addTaskBtn_OnClick(object sender, EventArgs e)
        {
            try
            {
                dao.AddTask(goalNameTask.Text, newTaskName.Text, newTAskDescription.Text);
            }
            catch (Exception ex)
            {
                ShowException(ex.Message);
            }
        }

        protected void addWeekBtn_OnClick(object sender, EventArgs e)
        {
            try
            {
                dao.AddWeek(Convert.ToInt32(weekAmount.Text));
            }
            catch (Exception ex)
            {
                ShowException(ex.Message);
            }
        }
    }
}